const path = require('path')
const fs = require('fs')
const titleConfig = require('./title.config')

function resolve (dir) {
  return path.join(__dirname, dir)
}

function getPages (pageName) {
  const map = {}
  if (!pageName) {
    const PAGE_PATH = path.resolve(__dirname, './src/pages')
    const entryFiles = fs.readdirSync(PAGE_PATH)
    // console.log(entryFiles)
    entryFiles.forEach(filePath => {
      map[filePath] = {
        entry: 'src/pages/' + filePath + '/main.js',
        // template: 'html/' + filePath + '.html',
        template: 'public/index.html',
        // filename: filePath + '.html',
        filename: filePath + '/' + filePath + '.html',
        title: titleConfig[filePath],
        chunks: ['chunk-vendors', 'chunk-common', 'element-ui', filePath]
      }
    })
    return map
  }
  // 指定单页面打包
  map[pageName] = {
    entry: 'src/pages/' + pageName + '/main.js',
    // template: 'html/' + pageName + '.html',
    template: 'public/index.html',
    filename: pageName + '/' + pageName + '.html',
    title: titleConfig[pageName],
    chunks: ['chunk-vendors', 'chunk-common', 'element-ui', pageName]
  }
  return map
}
console.log(process.env.NODE_ENV)
const rawArgv = process.argv.slice(2)
const [, , moduleName] = rawArgv
const pages = getPages(moduleName)

module.exports = {
  pages: pages,
  outputDir: 'dist',
  assetsDir: 'common',
  productionSourceMap: false, // 打包不生成map
  chainWebpack: config => {
    // 设置引入别名
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
      .set('@components', resolve('src/components'))
      .set('@client', resolve('src/pages/client/views'))
    //  设置js文件的打包出口
    config.output
      .filename(bundle => {
        // console.log(bundle.chunk.name)
        // return bundle.chunk.name === 'index' ? 'common/[name].js' : '[name]/[name].js'
        return '[name]/js/[name].js'
      })
    //  设置css文件的打包出口
    config.plugin('extract-css').tap(options => {
      options[0].filename = '[name]/css/[name].[hash:8].css'
      return options
    })
    //  设置img文件的打包出口
    config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .tap(options => {
        return Object.assign(options, {
          limit: 10240,
          fallback: {
            loader: 'file-loader',
            options: {
              name: 'common/img/[name].[hash:8].[ext]'
            }
          }
        })
      })
    // 使用splitChunks抽离公用代码
    config.optimization.splitChunks({
      cacheGroups: {
        common: {
          name: 'chunk-common', // 打包后的文件名
          chunks: 'initial', //
          minChunks: 2,
          maxInitialRequests: 5,
          minSize: 0,
          priority: 1,
          reuseExistingChunk: true
        },
        vendors: {
          name: 'chunk-vendors',
          test: /[\\/]node_modules[\\/]/,
          chunks: 'initial',
          priority: 2,
          reuseExistingChunk: true,
          enforce: true
        },
        'element-ui': {
          name: 'element-ui',
          test: /[\\/]node_modules[\\/]element-ui[\\/]/,
          chunks: 'initial',
          priority: 3,
          reuseExistingChunk: true,
          enforce: true
        }
      }
    })
  },
  css: {
    extract: true,
    requireModuleExtension: true,
    sourceMap: false
  }
}
